package com.zjun.demo.ruleview.slice;

import com.zjun.demo.ruleview.ResourceTable;
import com.zjun.widget.RuleView;
import com.zjun.widget.MoneySelectRuleView;
import com.zjun.widget.TimeRuleView;

import com.zjun.widget.ToastUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.agp.components.Button;
import ohos.agp.components.TextField;

import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.window.dialog.ToastDialog;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Main ability slice
 */
public class MainAbilitySlice extends AbilitySlice {
    private Text tv_rule_indicator;
    private Text tvValue;
    private RuleView gvRule;
    private Button btn_50;
    private Button btn_change;
    private MoneySelectRuleView msrvMoney;
    private float moneyBalance;
    private boolean isMoneySloped;
    private Text tvMoney;
    private Button btn_set_money;
    private Button btn_set_balance;
    private TextField etMoney;
    private TimeRuleView trvTime;
    private Text tvTime;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        findComponenById();
        tvValue.setText(Float.toString(gvRule.getCurrentValue()));
        click();
        dealListener();

        moneyBalance = msrvMoney.getBalance();
        msrvMoney.setOnValueChangedListener(
                new MoneySelectRuleView.OnValueChangedListener() {
                    @Override
                    public void onValueChanged(int newValue) {
                        tvMoney.setText(Integer.toString(newValue));
                        if (newValue > moneyBalance) {
                            if (!isMoneySloped) {
                                isMoneySloped = true;
                                // Snackbar.make(msrvMoney, "超出额度", Snackbar.LENGTH_SHORT).show();
                            }
                        } else {
                            if (isMoneySloped) {
                                isMoneySloped = false;
                            }
                        }
                    }
                });


        // 模拟时间段数据
        List<TimeRuleView.TimePart> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            TimeRuleView.TimePart part = new TimeRuleView.TimePart();
            part.startTime = i * 1000;
            part.endTime = part.startTime + new SecureRandom().nextInt(1000);
            list.add(part);
        }
        trvTime.setTimePartList(list);
    }

    private void click(){
        btn_50.setClickedListener(
                new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        gvRule.setCurrentValue(gvRule.getMinValue() == 11 ? 15 : 50);
                    }
                });

        btn_change.setClickedListener(
                new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        toggleValue();
                    }
                });

        tv_rule_indicator.setClickedListener(
                new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        toggleSettingsShow(ResourceTable.Id_ll_rule_settings);
                    }
                });

        btn_set_money.setClickedListener(
                new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        if(!isNumeric(etMoney.getText())){
                            ToastUtils.showToast(getContext(),"请输入数字");
                            return;
                        }
                        float money = getMoney();
                        msrvMoney.setValue(money);
                    }
                });

        btn_set_balance.setClickedListener(
                new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        moneyBalance = getMoney();
                        msrvMoney.setBalance(moneyBalance);
                        isMoneySloped = false;
                    }
                });
    }

    private void dealListener() {
        gvRule.setOnValueChangedListener(
                new RuleView.OnValueChangedListener() {
                    @Override
                    public void onValueChanged(float value) {
                        tvValue.setText(Float.toString(value));
                    }
                });



        trvTime.setOnTimeChangedListener(
                new TimeRuleView.OnTimeChangedListener() {
                    @Override
                    public void onTimeChanged(int newTimeValue) {
                        tvTime.setText(TimeRuleView.formatTimeHHmmss(newTimeValue));
                    }
                });
    }

    private void findComponenById() {
        tv_rule_indicator = (Text) findComponentById(ResourceTable.Id_tv_rule_indicator);
        tvValue = (Text) findComponentById(ResourceTable.Id_tv_value);
        gvRule = (RuleView) findComponentById(ResourceTable.Id_gv_1);
        btn_50 = (Button) findComponentById(ResourceTable.Id_btn_50);
        btn_change = (Button) findComponentById(ResourceTable.Id_btn_change);
        tvMoney = (Text) findComponentById(ResourceTable.Id_tv_money);
        btn_set_money = (Button) findComponentById(ResourceTable.Id_btn_set_money);
        btn_set_balance = (Button) findComponentById(ResourceTable.Id_btn_set_balance);
        msrvMoney = (MoneySelectRuleView) findComponentById(ResourceTable.Id_msrv_money);
        etMoney = (TextField) findComponentById(ResourceTable.Id_et_new_money);
        trvTime = (TimeRuleView) findComponentById(ResourceTable.Id_trv_time);
        tvTime = (Text) findComponentById(ResourceTable.Id_tv_time);
    }

    private void toggleValue() {
        if (gvRule.getMinValue() == 11) {
            gvRule.setValue(0, 100, 50, 0.1f, 10);
        } else {
            gvRule.setValue(11, 20, 15, 0.2f, 5);
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void toggleSettingsShow(int layoutId) {
        DirectionalLayout llSettings = (DirectionalLayout) findComponentById(layoutId);
        llSettings.setVisibility(
                llSettings.getVisibility() == Component.VISIBLE ? Component.INVISIBLE : Component.VISIBLE);
    }

    private float getMoney() {
        String moneyStr = etMoney.getText().toString();
        if (moneyStr.isEmpty()) {
            moneyStr = "0";
        }
        return Float.parseFloat(moneyStr);
    }

    /**
     * Is numeric boolean
     *
     * @param str str
     * @return the boolean
     */
    public  boolean isNumeric(String str){
        for (int i = str.length();--i>=0;){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }
}
